package cn.yj.config;

import cn.yj.supports.RabbitConfirmCallback;
import cn.yj.supports.RabbitReturnCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author 永健
 * @since 2022-02-09 10:05
 */
@Component
public class RabbitTemplateConfig implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof RabbitTemplate) {
            RabbitTemplate rabbitTemplate = (RabbitTemplate) bean;
            rabbitTemplate.setConfirmCallback(new RabbitConfirmCallback());
            rabbitTemplate.setReturnCallback(new RabbitReturnCallback());
            rabbitTemplate.addBeforePublishPostProcessors(message -> {
                message.getMessageProperties().setHeader("cn.yj", "781267910");
                return message;
            });
            return rabbitTemplate;
        }
        return bean;
    }
}
